package com.os;

import com.graphics.ImageAnimator;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class SpriteAnimator extends JPanel implements Runnable, MouseMotionListener, MouseListener {

    private Thread t;
    private int period = 50;        // 0.05 secs
    private ImageAnimator imageAnimator;
    private ArrayList sprite;
    private int width = 50;
    private int height = 50;
    private boolean running = false;
    private ArrayList bound;
    private Main comp;

    public SpriteAnimator(Main comp) {
        this.comp = comp;
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void start() {
        setRunning(true);
        t = new Thread(this);
        t.start();
    }

    public void stop() {
        setRunning(false);
        t.interrupt();
    }

    public void playAnim() {
        if (imageAnimator.isStopped()) {
            imageAnimator.resume();
        } else {
            imageAnimator.restartAt(0);
        }
    }

    public void pauseAnim() {
        imageAnimator.stop();
    }

    public void stopAnim() {
        imageAnimator.restartAt(0);
        imageAnimator.stop();
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    @Override
    public void run() {
        while (isRunning()) {
            try {
                Thread.sleep(period);
            } catch (Exception e) {
            }
            imageAnimator.updateTick();
            repaint();
        }
    }
    int indexSelected;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);
        drawImage(g2d, imageAnimator.getCurrentImage(), ObjectSpriteControlPanel.edgeX, ObjectSpriteControlPanel.edgeY);

        if (bound != null) {
            for (int i = 0; i < bound.size(); i++) {
                OSBoundingBox bb = (OSBoundingBox) bound.get(i);
                indexSelected = comp.ObjectSpriteListOfBound.getSelectedIndex();
                if (i == indexSelected) {
                    g2d.setColor(Color.red);
                } else {
                    g2d.setColor(Color.green);
                }
                g2d.drawRect(bb.x + ObjectSpriteControlPanel.edgeX, bb.y + ObjectSpriteControlPanel.edgeY, bb.width, bb.height);

            }
        }
        if (tmpDrag1 != null && tmpDrag2 != null) {
            g2d.setColor(Color.red);
            g2d.drawRect(x, y, w, h);
        }
    }

    private void drawImage(Graphics2D g2d, BufferedImage im, int x, int y) {
        g2d.drawImage(im, x, y, this);
    }

    public void setSprite(ArrayList sprite, int delay, boolean started) {
        this.sprite = sprite;
        imageAnimator = new ImageAnimator(period, delay, started, sprite);
    }

    public void setPeriod(int period) {
        this.period = period;
    }

    public int getPeriod() {
        return period;
    }

    @Override
    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void setBound(ArrayList bound) {
        this.bound = bound;
    }

    public ArrayList getBound() {
        return bound;
    }
    private Point tmpDrag1;
    private Point tmpDrag2 = new Point();
    private Point tmpDrag3 = new Point();
    int x, y, w, h;

    @Override
    public void mouseDragged(MouseEvent e) {
        if (tmpDrag1 == null) {
            tmpDrag1 = new Point(e.getX(), e.getY());
        } else {
            tmpDrag2 = new Point(e.getX(), e.getY());
            x = Math.min(tmpDrag1.x, tmpDrag2.x);
            y = Math.min(tmpDrag1.y, tmpDrag2.y);
            w = Math.abs(tmpDrag1.x - tmpDrag2.x);
            h = Math.abs(tmpDrag1.y - tmpDrag2.y);
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (tmpDrag1 != null) {
            OSBoundingBox osb = new OSBoundingBox();
            osb.setBounds(x - ObjectSpriteControlPanel.edgeX, y - ObjectSpriteControlPanel.edgeY, w, h);
            comp.osControlPanel.addBoundingBox(osb);
            tmpDrag1 = null;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
